<template>
  <div class="login">
    <div class="login-form">
      <a-spin style="width: 100%;height: 100%;"
              tip="正在登录，请稍后..."
              :spinning="spinning">
        <div class="login-form-title">
          维修宝后台管理系统登录
          <a-divider></a-divider>
        </div>
        <a-form-model :model="form"
                      :rules="rules"
                      ref="loginForm">
          <a-form-model-item label="用户名" prop="username">
            <a-input v-model="form.username" placeholder="请输入用户名">
              <a-icon slot="prefix" type="user"/>
            </a-input>
          </a-form-model-item>
          <a-form-model-item label="密码" prop="password">
            <a-input-password v-model="form.password" placeholder="请输入密码">
              <a-icon slot="prefix" type="lock"/>
            </a-input-password>
          </a-form-model-item>
          <a-form-model-item label="验证码" prop="verifyCode">
            <a-input v-model="form.verifyCode" placeholder="请输入验证码" @keyup.enter="submitForm">
              <a-icon slot="prefix" type="code"/>
              <img
                ref="verifyCode"
                slot="addonAfter" 
                style="width: 90px;height: 28px;" 
                title="看不清？点我换一张"
                src=""
                @click="getVerifyCodeFn"
              >
            </a-input>
          </a-form-model-item>
          <a-form-model-item style="float:right;" prop="remember">
            <a-checkbox @click="form.remember = !form.remember" :checked="form.remember">
              记住账号
            </a-checkbox>
          </a-form-model-item>
          <a-form-model-item>
            <a-button type="primary" style="width: 100%;margin-top: 20px;" @click="submitForm">登录</a-button>
            <a-button style="width: 100%;" @click="resetForm">重置</a-button>
          </a-form-model-item>
        </a-form-model>
      </a-spin>
    </div>
  </div>
</template>

<script>
import {login, getVerifyCode} from "@/api/api";
import { mapMutations } from 'vuex';

export default {
  name: "Login",
  data() {
    return {
      spinning: false,
      form: {
        username: 'xzh',
        password: 'xzh123',
        verifyCode: '',
        remember: false,
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
        verifyCode: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
        ],
      },
    }
  },
  methods: {
    ...mapMutations(['updateAdminInfo']),
    /**
     * 获取验证码
     */
    getVerifyCodeFn() {
      this.$refs['verifyCode'].src = getVerifyCode() + `?timestamp=${new Date().getTime()}`;
    },
    /**
     * 表单登录
     */
    submitForm() {
      this.spinning = true;
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          let options = {
            name: this.form.username,
            password: this.form.password,
            code: this.form.verifyCode,
          }
          // 判断：若选择记住账号，则将账号存入localStorage
          if (this.form.remember) {
            localStorage.username = this.form.username;
          }
          // 登录
          login(options)
            .then(res => {
              this.updateAdminInfo(res.data);
              localStorage.setItem('access_token', res.data.token);
              this.$router.push('/home/index');
              this.$store.commit('setCurRouter', '/home/index');
            })
            .catch(err => {
              this.$message.error(err.data);
              this.getVerifyCodeFn();
            })
            .finally(() => {
              this.spinning = false;
            })
        } else {
          this.$message.error('登陆失败，缺乏关键信息');
        }
      })
    },
    /**
     * 表单重置
     */
    resetForm() {
      this.$refs.loginForm.resetFields();
    },
  },
  mounted() {
    if (localStorage.username && localStorage.username !== '') {
      this.form.username = localStorage.username;
    }
    this.getVerifyCodeFn();
  }
}
</script>

<style lang="scss">
.login {
  width: 100%;
  height: 100%;
  background: url("../assets/img/login-bg.jpg") no-repeat;
  background-size: 100%;

  .login-form {
    width: 380px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -55%);
    padding: 20px;
    border-radius: 6px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
    background-color: rgba(255, 255, 255, .6);

    .login-form-title {
      text-align: center;
      font-size: 20px;
      letter-spacing: 2px;
      line-height: 24px;
      margin-bottom: 4px;

      .ant-divider-horizontal {
        margin: 10px 0;
      }
    }

    .ant-form-item {
      margin-bottom: 2px;

      .ant-form-item-label {
        line-height: 30px;
      }

      .ant-input-group-addon {
        padding: 0;
      }
    }
  }
}
</style>